/**
 * Project Name:fire-industry-DM-center
 * File Name:AreaServer.java
 * Package Name:com.firestone.web.area
 * Date:2018年4月19日下午4:57:45
 *
*/

package com.firestone.web.area;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.plugins.Page;
import com.firestone.bean.dto.PageDTO;
import com.firestone.bean.dto.ReturnDTO;
import com.firestone.bean.dto.area.AreaDTO;
import com.firestone.bean.dto.area.SearchDTO;
import com.firestone.bean.dto.user.User;
import com.firestone.bean.entity.mysql.primary.ImArea;
import com.firestone.bean.po.area.ListPO;
import com.firestone.bean.po.area.SavePO;
import com.firestone.bean.vo.BaseListVO;
import com.firestone.bean.vo.area.AreaVO;
import com.firestone.converters.area.ConverterArea;
import com.firestone.core.enums.SystemCodeEnum;
import com.firestone.core.utils.OSSUtil;
import com.firestone.service.area.IImAreaService;
import com.firestone.web.BaseServer;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @Description: TODO(项目控制器)
 * @author CAIYJ
 * @date 2018年4月19日 下午4:57:45
 */
@Api(value = "项目控制器", tags = { "项目接口文档" })
@RestController
@RequestMapping(value = "/area")
public class AreaServer extends BaseServer {

    @Autowired
    private IImAreaService imAreaService;

    /**
     * @Description: TODO(项目列表)
     * @param dto
     * @param po
     * @return
     * @throws Exception
     * @author CAIYJ
     * @date 2018年4月20日 上午9:39:35
     * @lastModifier CAIYJ
     */
    @ApiOperation(value = "项目列表", notes = "项目列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @RequestMapping(value = "/list")
    public ReturnDTO<BaseListVO<AreaVO>> list(PageDTO dto, ListPO po)
            throws Exception {
        Page<AreaDTO> page = new Page<>(dto.getPage(), dto.getSize());
        SearchDTO search = new SearchDTO();
        BeanUtils.copyProperties(po, search);
        page = imAreaService.listArea(page, search);
        return ReturnDTO.success(ConverterArea.convertListVO(page));
    }

    /**
     * @Description: TODO(新增项目)
     * @param savePO
     * @return
     * @throws Exception
     * @author CAIYJ
     * @date 2018年4月20日 上午9:57:20
     * @lastModifier CAIYJ
     */
    @ApiOperation(value = "新增项目", notes = "新增项目", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @RequestMapping(value = "/save")
    public ReturnDTO<String> save(SavePO savePO) throws Exception {
        ImArea area = new ImArea();
        if (savePO.getBrainLogo() != null) {
            String logo = OSSUtil.uploadObject2OSS(savePO.getBrainLogo());
            area.setBrainLogo(logo);
        }
        if (savePO.getMapLogo() != null) {
            String logo = OSSUtil.uploadObject2OSS(savePO.getMapLogo());
            area.setMapLogo(logo);
        }
        User user = getUserInfo();
        BeanUtils.copyProperties(savePO, area);
        area.setCreator(user.getId());
        if (imAreaService.insert(area)) {
            return ReturnDTO.success("添加成功");
        }
        return ReturnDTO.fail(SystemCodeEnum.error_create_failed);
    }
}
